
#include <BClass>
#include "member_BClass.h"
#include "member_BSource.h"

using namespace BWE;

#define member					(*(member_BClass*)_ptr)
#define member_allocate()		_ptr = new member_BClass(this)
#define member_release()		delete (member_BClass*)_ptr

BClass::BClass()
{
	member_allocate();
}
BClass::~BClass()
{
	member_release();
}

bool BClass::isMeta() const
{
	return member.meta;
}
const BString& BClass::type() const
{
	return member.type;
}

bool BClass::insertMethod(const char* declare, CFunc cfunc)
{
	BString type;
	BString name;
	BStringList types;
	if (!AnalyseDeclare(declare, type, name, types))
		return false;
	if (member.methodMap.contain(name))
		return false;
	Method* method = new Method();
	method->type = type;
	method->cfunc = cfunc;
	method->params.resize(types.size());
	for (int i = 0; i < types.size(); i++)
	{
		BString& word = types[i];
		method->params[i].setType(word);
	}
	member.methodMap[name] = method;
	return true;
}
bool BClass::insertMethod(const BString& declare, CFunc cfunc)
{
	BString type;
	BString name;
	BStringList types;
	if (!AnalyseDeclare(declare, type, name, types))
		return false;
	if (member.methodMap.contain(name))
		return false;
	Method* method = new Method();
	method->type = type;
	method->cfunc = cfunc;
	method->params.resize(types.size());
	for (int i = 0; i < types.size(); i++)
	{
		BString& word = types[i];
		method->params[i].setType(word);
	}
	member.methodMap[name] = method;
	return true;
}
bool BClass::removeMethod(const char* declare)
{
	BString type;
	BString name;
	if (!AnalyseDeclare(declare, type, name))
		return false;
	return member.methodMap.remove(name);
}
bool BClass::removeMethod(const BString& declare)
{
	BString type;
	BString name;
	if (!AnalyseDeclare(declare, type, name))
		return false;
	return member.methodMap.remove(name);
}
bool BClass::containMethod(const char* declare)
{
	BString type;
	BString name;
	if (!AnalyseDeclare(declare, type, name))
		return false;
	return member.methodMap.contain(name);
}
bool BClass::containMethod(const BString& declare)
{
	BString type;
	BString name;
	if (!AnalyseDeclare(declare, type, name))
		return false;
	return member.methodMap.contain(name);
}

int BClass::methodCount() const
{
	return member.methodMap.size();
}
const BString& BClass::method(int index) const
{
	auto it = member.methodMap.seek(index);
	return it.key();
}

bool BClass::insertVariant(const char* type, const char* name)
{
	if (member.variantMap.contain(name))
		return false;
	auto it = member.variantMap.insert(name);
	it->setType(type);
	it->setName(name);
	return true;
}
bool BClass::insertVariant(const BString& type, const BString& name)
{
	if (member.variantMap.contain(name))
		return false;
	auto it = member.variantMap.insert(name);
	it->setType(type);
	it->setName(name);
	return true;
}

bool BClass::removeVariant(const char* name)
{
	return member.variantMap.remove(name);
}
bool BClass::removeVariant(const BString& name)
{
	return member.variantMap.remove(name);
}
bool BClass::removeVariant(const char* type, const char* name)
{
	auto it = member.variantMap.find(name);
	if (it.valid() && it->type() == type)
	{
		return member.variantMap.erase(it);
	}
	return false;
}
bool BClass::removeVariant(const BString& type, const BString& name)
{
	auto it = member.variantMap.find(name);
	if (it.valid() && it->type() == type)
	{
		return member.variantMap.erase(it);
	}
	return false;
}

bool BClass::containVariant(const char* name) const
{
	return member.variantMap.contain(name);
}
bool BClass::containVariant(const BString& name) const
{
	return member.variantMap.contain(name);
}

int BClass::variantCount() const
{
	return member.variantMap.size();
}
BVariant* BClass::variant(int index)
{
	BVariant& var = member.variantMap.seek(index).value();
	return &var;
}
const BVariant* BClass::variant(int index) const
{
	const BVariant& var = member.variantMap.seek(index).value();
	return &var;
}

BVariant* BClass::variant(const char* name)
{
	auto it = member.variantMap.find(name);
	if (it.valid())
		return &it.value();
	return 0;
}
BVariant* BClass::variant(const BString& name)
{
	auto it = member.variantMap.find(name);
	if (it.valid())
		return &it.value();
	return 0;
}

bool BClass::input(const BString& text, const BValue& value)
{
	if (text == "inherit")
	{

	}
	return BObject::input(text, value);
}
bool BClass::query(const BString& text, BValue& value) const
{
	if (text == "inherit")
	{

	}
	return BObject::query(text, value);
}
